package org.cos.verifier.ui;

import java.awt.Adjustable;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.LayoutManager;
import java.awt.List;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.Array;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import org.cos.verifier.process.ProfileManager;
import org.cos.verifier.process.Rule;
import org.cos.verifier.process.RuleManager;

public class RuleMgrDlg extends Frame {
	List rules_list;
	RuleMgrDlg me;
	Label title;
	Panel ctrl_panel;
	Panel list_panel;
	Panel rules_panel;
	ScrollPane list_panel_wrap;
	Button edit_btn, save_btn, cancel_btn;
	Vector chks = null;
	String crr_name = null;
	TextArea rule_describ ;

	public RuleMgrDlg() {
		this.me = this;
		chks = new Vector();
		me.setLayout(new BorderLayout());
		rule_describ = new TextArea();
		title = new Label("Cos-Verifier 规则管理器");
		me.add(title, BorderLayout.NORTH);
		list_panel = new Panel();
		list_panel.setLayout(new GridLayout(0, 1));
		
		Hashtable rs = RuleManager.get_inst().get_all_rule();
		Hashtable profile = ProfileManager.get_inst().crr_profile();
		
		ProfileManager.get_inst().sync(rs);
		for (Enumeration ex = rs.elements(); ex.hasMoreElements(); ){
			Rule r = (Rule) ex.nextElement();
			Checkbox chk_box = new Checkbox(r.name);
			chk_box.setName(r.name);
			chk_box.addFocusListener(new FocusListener(){
				public void focusGained(FocusEvent e) {
					crr_name = e.getComponent().getName();
					rule_describ.setText(RuleManager.get_inst().get_rule(crr_name).describ);
				}
				public void focusLost(FocusEvent e) {
					
				}	
			
			});
			chk_box.setState(((Integer)profile.get(r.name)).intValue()==1);
			
			chks.add(chk_box);
			list_panel.add(chk_box);
		}
		
		list_panel_wrap = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
		list_panel_wrap.add(list_panel);
		rules_panel = new Panel();
		rules_panel.setLayout(new GridLayout(2,1));
		rules_panel.add(list_panel_wrap);
		rules_panel.add(rule_describ);
		me.add(rules_panel, BorderLayout.CENTER);
		
		ctrl_panel = new Panel();
		ctrl_panel.setLayout(new GridLayout(2,3));
		
		edit_btn = new Button("编辑");
		edit_btn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				RuleEditListDlg rel_dlg = new RuleEditListDlg();
				rel_dlg.display(me);
				me.dispose();
			}
		});
		
		save_btn = new Button("保存");
		save_btn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				Hashtable profile = ProfileManager.get_inst().crr_profile();
				String profile_name = ProfileManager.get_inst().crr_profile_name();
				for (Enumeration e = chks.elements(); e.hasMoreElements();){
					Checkbox chk_box = (Checkbox) e.nextElement();
					String name = chk_box.getLabel();
					Integer checked = new Integer(chk_box.getState()?1:0);
					profile.put(name, checked);
					LogDlg.get_inst().log("[Prof.`"+profile_name+"']"+name+"->"+checked);
							
				}
				ProfileManager.get_inst().dump();
				me.dispose();
			}
		});
		
		cancel_btn = new Button("取消");
		cancel_btn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				me.dispose();
			}
		});
		
		ctrl_panel.add(save_btn);
		ctrl_panel.add(cancel_btn);
		ctrl_panel.add(edit_btn);
		ctrl_panel.add(new Label());
		ctrl_panel.add(new Label());


		me.add(ctrl_panel, BorderLayout.SOUTH);
		me.setSize(400, 500);
		
		
		me.addWindowListener(new FrameHandler());
	}
	
	public void display(){
		me.setVisible(true);
	}

	
	class FrameHandler extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			me.dispose();
		}
	}
	
}
